package utils;

import java.awt.List;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.TreeSet;

public class OverlapingUtil {
	
	private class Segment {
		public double value;
		public boolean type;
		public Segment(double value, boolean type) {
			this.value = value;
			this.type = type;
		}
		
	}
	
	Comparator<Segment> mComp = new Comparator<Segment>() {

		@Override
		public int compare(Segment o1, Segment o2) {
			return (int)((o1.value - o2.value) * 10 );
		}
	};
	
	
	
	private LinkedList<Segment> mSegments;
	
	public OverlapingUtil() {
		mSegments = new LinkedList<Segment>();
	}
	
	public void addSegment(int start, int end) {
		mSegments.add(new Segment((double)start + 0.1, true));
		mSegments.add(new Segment((double)end - 0.1, false));
		Collections.sort(mSegments, mComp);
	}
	
	public boolean isOverlapping() {
		if (mSegments.isEmpty()) {
			return false;
		}
		
		boolean expected = true;
				
		for (Segment s : mSegments) {
			if (s.type != expected) {
				return true;
			}
			expected = !expected;
		}
		
		return false;
	}
	

}

